package com.sfh.cp.mass.child;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sfh.cp.base.RefreshRecyleFragment;
import com.sfh.cp.home.child.BaseChildNoteFragment;
import com.sfh.cp.home.child.adapter.ContentAdapter;
import com.sfh.cp.mass.MassAddMessageActivity;
import com.sfh.cp.mass.pst.IPstNote;
import com.sfh.cp.mass.req.ReqPageNote;
import com.sfh.cp.mass.req.RxNoteMessageSize;
import com.sfh.cp.widget.WidgetContentImageLayout;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.annotation.RxBusEvent;
import com.sfh.lib.mvp.service.LoaderService;

import java.util.List;

import crm.sfh.server.entity.EContent;
import crm.sfh.server.entity.ENote;
import crm.sfh.server.entity.rsp.TPage;

/**
 * 最热
 */

public class GroomFragment extends BaseChildNoteFragment implements IPstNote.IVNote {


    public static GroomFragment newFragment(String massId) {
        GroomFragment fragment = new GroomFragment();
        Bundle bundle = new Bundle();
        bundle.putString("DATA", massId);
        fragment.setArguments(bundle);
        return fragment;
    }

    String massId;

    IPstNote pstMassGroom;

    @Override
    public BaseQuickAdapter createAdapter() {
        return new ContentAdapter(this);
    }

    @Override
    public boolean loadMore() {
        return true;
    }

    @Override
    public void onRefreshUI() {
        if (pstMassGroom == null) {
            return;
        }
        if (TextUtils.isEmpty(massId)) {
            massId = getArguments().getString("DATA");
        }
        nowPage = 1;
        ReqPageNote req = new ReqPageNote(nowPage);
        req.addWhereEqualTo("massId", massId);
        req.setOrder("-sizeReading");
        pstMassGroom.listPage(req);
    }

    @Override
    public void onLoadMoreUI() {
        if (pstMassGroom == null) {
            return;
        }
        if (TextUtils.isEmpty(massId)) {
            massId = getArguments().getString("DATA");
        }
        nowPage += 1;
        ReqPageNote req = new ReqPageNote(nowPage);
        req.addWhereEqualTo("massId", massId);
        req.setOrder("-sizeReading");
        pstMassGroom.listPage(req);
    }

    @Nullable
    @Override
    public IPresenter getPresenter() {
        if (pstMassGroom == null) {
            pstMassGroom = LoaderService.getService(IPstNote.class);
        }
        return pstMassGroom;
    }

    @Override
    public void onSuccess(TPage<EContent> page) {
        success(page);
    }

    @Override
    public void onError(String error) {

        error("", error);
    }


    @Override
    public void onClickFabulous(View v, ENote contentType) {

        pstMassGroom.updateFabulous(contentType);
    }

    @Override
    public void onClickMessage(View v, ENote contentType) {

        MassAddMessageActivity.startUI(getActivity(), contentType);
    }

    @Override
    public void onClickImage(View v, ENote contentType, int position) {

    }

    @RxBusEvent(eventClass = RxNoteMessageSize.class, taskId = 10021)
    public void onUpdateMessageSize(RxNoteMessageSize size) {
        List<EContent> list = adapter.getData();
        for (int i = 0; i < list.size(); i++) {
            ENote note =  list.get(i).getContentType();

            if (TextUtils.equals(size.getNoteId(), note.getNoteId())) {
                note.setSizeMessage(note.getSizeMessage() + 1);
                adapter.notifyItemChanged(i);
                break;
            }
        }
    }
}
